package com.traffic.between.adaptor;

import java.util.Date;
import org.apache.log4j.Logger;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.XieMeiServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel = "xmkj")
public class XieMeiAdaptor implements IAdaptor{
	private static Logger log = Logger.getLogger(XieMeiAdaptor.class);


	public void order() {

		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		
		
		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo = orderBean.getIds();
		log.info("bsm- 协美科技流量订购接口请求参数productId:" + productId+",phoneNo:"+phoneNo+",cstmOrderNo:"+cstmOrderNo);
		String result = XieMeiServiceImpl.service.order(productId, phoneNo, cstmOrderNo);
		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("Code");
		String requestNo = (String) json.get("Request_no");
	
		

		if (("00000".equals(code))) {
			
			orderBean.setMessage((String) json.get("Msg"));
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("11");
			orderBean.setSuppOrderNo(requestNo);
		} else {
			orderBean.setMessage((String) json.get("Msg"));
			orderBean.setErrorMsg("订购失败");
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {

		/**
		 * 0：订单已受理，1：订购中，7：订购成功，8：订购失败 { "code": "0000", "msg": "成功", "data": {
		 * "orderNo": "GDP14656954660051458", "cstmOrderNo": "test1234567"
		 * "status": "8", "errorDesc": "该用户是黑名单用户" } }
		 * 
		 */
		OrderBean orderBean = OrderInfoContext.getContext();
		

		String cstmOrderNo = orderBean.getSuppOrderNo();
		log.info("bsm -协美科技流量订购状态查询接口请求参数cstmOrderNo ：" + cstmOrderNo);
		if ("".equals(cstmOrderNo)) {
			orderBean.setMessage("该订单业务流水为空");
			orderBean.setErrorMsg("受理中，无订单");
			orderBean.setStatus("");
		}else {
			String result = XieMeiServiceImpl.service.orderStatus(cstmOrderNo);

			JSONObject json = JSONObject.fromObject(result);

			String code = (String) json.get("Code");

			if ("11111".equals(code)) {
				orderBean.setMessage((String) json.get("Msg"));
				orderBean.setErrorMsg("充值成功");
				orderBean.setStatus("7");
			} else if ("11112".equals(code)) {
				orderBean.setMessage((String) json.get("Msg"));
				orderBean.setErrorMsg("运营商还没返回结果,请等待");
				orderBean.setStatus("1");
			} else {
				orderBean.setMessage((String) json.get("Msg"));
				orderBean.setErrorMsg("订购失败");
				orderBean.setStatus("8");
			}
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
